package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CalServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost() 被调用...");
        //接收提交的数据并进行计算
        req.setCharacterEncoding("UTF-8");
        String num1 = req.getParameter("num1");
        String num2 = req.getParameter("num2");
        int res = Integer.parseInt(num1) + Integer.parseInt(num2);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print("<h1>结果是：" + res + "<h1/>");
        out.flush();
        out.close();
    }
}
